
// gcc cjson/cJSON.c test.c -lm

#include <stdio.h>
#include <stdlib.h>
#include "cjson/cJSON.h"

/* Parse text to JSON, then render back to text, and print! */
void test_prient(char *text)
{
	char *out;
	cJSON *json;

	if (!json) {
		printf("Error before: [%s]\n",cJSON_GetErrorPtr());
	} else {
		out=cJSON_Print(json);
		cJSON_Delete(json);
		printf("%s\n",out);
		free(out);
	}
}

void test_array(char *text)
{
	cJSON *json;
	cJSON *people;
	cJSON *man;

	char *name;
	
	json = cJSON_Parse(text);

	people = cJSON_GetObjectItem(json, "people");

	int size = 0;
	size = cJSON_GetArraySize(people);
	printf("size is %d \n",size);

	man = cJSON_GetArrayItem(people, 0);

	// cJSON *item=cJSON_GetObjectItem(man,"firstName"); 

	// printf("man is %s \n", item->valuestring);

	name = cJSON_GetStringFromObject(man, "firstName");

	printf("man is %s \n", name);

	cJSON_Delete(json);
}

int main (int argc, const char * argv[]) {
	/* a bunch of json: */
	char text[]="{\"people\":[{\"firstName\":\"z\",\"lastName\":\"Jason\",\"email\":\"bbbb@126.com\",\"height\":1.67},{\"lastName\":\"jadena\",\"email\":\"jadena@126.com\",\"age\":8,\"height\":1.17},{\"email\":\"cccc@126.com\",\"firstName\":\"z\",\"lastName\":\"Juliet\",\"age\":36,\"height\":1.55}]}";

	/* Process each json textblock by parsing, then rebuilding: */
	test_array(text);
	
	return 0;
}
